/*
 * Copyright (c) 2017 22420103@QQ.COM 版权所有
 */
package com.lehe.service.impl;

import com.lehe.common.Contants;
import com.lehe.common.exceptions.LHAdminException;
import com.lehe.common.utils.StringUtil;
import com.lehe.dao.AdminUserDao;
import com.lehe.entry.AdminUserDto;
import com.lehe.entry.AdminUserMappingDto;
import com.lehe.entry.BaseResponse;
import com.lehe.entry.SqlParameterDto;
import com.lehe.service.impl.AbstractService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 类的功能描述
 *
 * @author 王丹
 * @since 2017-03-07
 */
@Service
@Transactional
public class AdminUserService extends AbstractService {

    @Autowired
    protected AdminUserDao adminUserDao;
    @Autowired
    protected AdminUserMappingService adminUserMappingService;

    public AdminUserDto queryById(Long adminUserId){
        return adminUserDao.queryById(adminUserId);
    }

    public AdminUserDto queryByAccount(String account){
        SqlParameterDto sqlParameterDto = new SqlParameterDto();
        sqlParameterDto.setAccount(account);
        List<AdminUserDto> adminUserDtoList = adminUserDao.queryAdminUserBymult(sqlParameterDto);
        if(null!=adminUserDtoList && !adminUserDtoList.isEmpty()){
            return adminUserDtoList.get(0);
        }
        return null;
    }

    public AdminUserDto queryByIdOrAccount(String seachStr){
        AdminUserDto userDto = null;
        userDto = queryByAccount(seachStr);
        if(null == userDto){
            userDto = queryById(Long.parseLong(seachStr));
        }
        return userDto;
    }

    public List<AdminUserDto> queryAdminUserBymult(SqlParameterDto sqlParameterDto){
        return adminUserDao.queryAdminUserBymult(sqlParameterDto);
    }

    public Long queryAdminUserCountBymult(SqlParameterDto sqlParameterDto){
        return adminUserDao.queryAdminUserCountBymult(sqlParameterDto);
    }

    public Integer updateAdminUserById(AdminUserDto adminUserDto){
        return adminUserDao.updateAdminUserById(adminUserDto);
    }

    public BaseResponse insertAdminUser(AdminUserDto adminUserDto){
        BaseResponse baseResponse = new BaseResponse(BaseResponse.FAIL, "保存失败");
        if(StringUtil.isBlank(adminUserDto.getAccount())){
            baseResponse.setMsg("登录账号不能为空");
            return baseResponse;
        }
        if(StringUtil.isBlank(adminUserDto.getPwd())){
            baseResponse.setMsg("登录密码不能为空");
            return baseResponse;
        }
        if(StringUtil.isBlank(adminUserDto.getName())){
            baseResponse.setMsg("用户姓名不能为空");
            return baseResponse;
        }
        if(StringUtil.isBlank(adminUserDto.getUserIds())){
            baseResponse.setMsg("关联玩家不能为空");
            return baseResponse;
        }
        adminUserDto.setStatus(Contants.ADMIN_USER_STATUS_ACTIVE);
        adminUserDto.setPwd(DigestUtils.md5Hex(adminUserDto.getPwd()));
        if(null!=queryByAccount(adminUserDto.getAccount())){
            baseResponse.setMsg("登录账号已经存在");
            return baseResponse;
        }
        adminUserDao.insertAdminUser(adminUserDto);
        String[] userIds = null;
        if(!adminUserDto.getUserIds().equals("0")){
            userIds = adminUserDto.getUserIds().split(",");
            AdminUserMappingDto[] adminUserMappingDtos = new AdminUserMappingDto[userIds.length];
            for(int i=0;i<userIds.length;i++){
                String userId = userIds[i];
                AdminUserMappingDto adminUserMappingDto = new AdminUserMappingDto();
                adminUserMappingDto.setAdminUserId(adminUserDto.getId());
                adminUserMappingDto.setUserId(Long.parseLong(userId));
                adminUserMappingDtos[i] = adminUserMappingDto;
            }
            try {
                adminUserMappingService.insertAdminUserMapping(adminUserMappingDtos);
            } catch (LHAdminException e) {
                logger.error("新增关联用户异常", e);
                baseResponse.setMsg(e.getMessage());
                return baseResponse;
            }
        }
        baseResponse.setCode(BaseResponse.SUCCESS);
        return baseResponse;
    }

    public BaseResponse delUserById(Long adminUserId){
        BaseResponse baseResponse = new BaseResponse(BaseResponse.FAIL, "删除失败");
        if(null == adminUserId){
            baseResponse.setMsg("用户ID不能为空");
            return baseResponse;
        }

        adminUserDao.deleteById(adminUserId);
        adminUserMappingService.deleteByAdminUserId(adminUserId);
        baseResponse.setCode(BaseResponse.SUCCESS);
        return baseResponse;
    }
}
